package com.hsd.znsh.controller;

import com.hsd.znsh.code.ValidateCodeProcessorHolder;
import com.hsd.znsh.code.sms.SmsCodeSender;
import com.hsd.znsh.entity.Gateway;
import com.hsd.znsh.enums.OnlineStatusEnum;
import com.hsd.znsh.exception.ZnshException;
import com.hsd.znsh.pojo.input.ForgetPasswordPhone;
import com.hsd.znsh.pojo.output.ResultVO;
import com.hsd.znsh.properties.SecurityProperties;
import com.hsd.znsh.service.GatewayService;
import com.hsd.znsh.service.UserService;
import com.hsd.znsh.utils.ResultVOUtil;
import com.hsd.znsh.utils.ValidatorUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StreamUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-05-07 14:13:57
 */
@Controller
@RequestMapping(value={"/function","/app"})
public class FunctionController {

    @Autowired
    private UserService userService;

    @Autowired
    private SecurityProperties securityProperties;

    @PostMapping("/forgetPassword")
    @ResponseBody
    public ResultVO forgetPassword(@Valid ForgetPasswordPhone passwordPhone, BindingResult bindingResult){
        ValidatorUtil.valid(bindingResult);
        userService.forgetPassword(passwordPhone);
        return ResultVOUtil.success("修改密码成功");
    }

    @GetMapping("/getFarmImage")
    public ResponseEntity<byte[]> getFarmImage(String path)  throws IOException {
        String filePath=securityProperties.getBrowser().getFilePath();
        if(!path.startsWith(filePath+"/farm")) throw new ZnshException(500,"文件路径不正确");
        InputStream inputStream=new FileInputStream(path);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        byte[] bytes= StreamUtils.copyToByteArray(inputStream);
        inputStream.close();
        return new ResponseEntity<byte[]>(bytes, headers, HttpStatus.OK);
    }

    @GetMapping("/getSensorImage")
    public ResponseEntity<byte[]> getSensorImage(String path) throws IOException {
        String filePath=securityProperties.getBrowser().getFilePath();
        if(!path.startsWith(filePath+"/sensor")) throw new ZnshException(500,"文件路径不正确");
        InputStream inputStream = new FileInputStream(path);
        HttpHeaders headers=new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        byte[] bytes=StreamUtils.copyToByteArray(inputStream);
        inputStream.close();
        return new ResponseEntity<byte[]>(bytes,headers,HttpStatus.OK);
    }

    @GetMapping("/**")
    public String toPage(HttpServletRequest request){
        Enumeration<String> parameterNames=request.getParameterNames();
        while(parameterNames.hasMoreElements()){
            String string=parameterNames.nextElement();
            request.setAttribute(string,request.getParameter(string));
        }
        System.out.println(StringUtils.substringAfter(request.getRequestURI(),request.getContextPath()+"/function/"));
        return StringUtils.substringAfter(request.getRequestURI(),request.getContextPath()+"/function/");
    }
}
